package org.theqcp.wow.model.api;

/**
 * Allows one level of progress listener to listen to the next level
 * as a subset of itself without having to send some information on
 * the current state of progress or how much it needs to fill.
 * 
 * @author angryundead
 *
 */
public class ProgressListenerAdapter implements ProgressListener {

	private ProgressListener listener; 
	
	private double chunk;
	
	private double base;
	
	private ProgressListenerAdapter() {

	}
	
	public ProgressListenerAdapter(final ProgressListener listener, double from, double to) {
		//construct
		this();
	
		//get listener
		this.listener = listener;
		
		if(from > to) {
			this.base = to;
		} else {
			this.base = from;
		}
		
		//chunk
		this.chunk = Math.abs(from - to);
	}
	
	@Override
	public void updateProgress(double d) {
		double update = this.chunk * (d / 100);
		if(this.listener != null) {
			this.listener.updateProgress(this.base + update);
		}
	}

	@Override
	public void updateProgress(double d, String message) {
		double update = this.chunk * (d / 100);
		if(this.listener != null) {
			this.listener.updateProgress(this.base + update, message);		
		}
	}

	@Override
	public void complete() {
		this.updateProgress(100);
	}

	@Override
	public void fail() {
		//do nothing
	}

}
